home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Source.bin / InvisibleHTMLLink.java < prev    next >
Text File  |  1998-08-21  |  10KB  |  325 lines

  1. package symantec.itools.awt;
  2.  
  3. import java.net.URL;
  4. import java.awt.Container;
  5. import java.applet.*;
  6. import java.beans.PropertyVetoException;
  7. import java.beans.PropertyChangeListener;
  8. import java.beans.VetoableChangeListener;
  9. import java.awt.event.ActionEvent;
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.MouseEvent;
  12. import java.awt.event.MouseListener;
  13.  
  14. //     06/11/97    LAB    Updated to Java 1.1.
  15. //     07/18/97    LAB    Added add/removeNotify to handle event listener registration.
  16. //  07/29/97    CAR marked fields transient as needed
  17. //                  inner adaptor class implements java.io.Serializable
  18. //  08/18/97    CAR moved code to locate containing Applet into addNotify
  19. //     08/21/97    LAB    Updated so when the mouse enters, the HTML link is displayed in the 
  20. //                    status area of the browser/viewer.  Added a check in the action event
  21. //                    handling code to look for a null url.  Apparently context.showDocument()
  22. //                    would silently throw, causing the action event to be eaten if it was passed
  23. //                    a null url.  Addresses Mac Bug #7436
  24.  
  25. /**
  26.  * Use this component to create an invisible rectangular button, usually within
  27.  * an image, that displays the document at a given URL when clicked.
  28.  * @version 1.1, June 11, 1997
  29.  * @author Symantec
  30.  */
  31. public class InvisibleHTMLLink extends InvisibleButton
  32. {
  33.     /**
  34.      * Constructs a default InvisibleHTMLLink.
  35.      */
  36.     public InvisibleHTMLLink()
  37.     {
  38.         frame    = null;
  39.         url        = null;
  40.         context = null;
  41.     }
  42.  
  43.     /**
  44.      * Sets the URL of the document to show when the button is clicked.
  45.      * @param u the URL
  46.      * @exception PropertyVetoException
  47.      * if the specified property value is unacceptable
  48.      * @see #getURL
  49.      */
  50.     public void setURL(URL u) throws PropertyVetoException
  51.     {
  52.         if(!symantec.itools.util.GeneralUtils.objectsEqual(url, u))
  53.         {
  54.             URL oldValue = url;
  55.  
  56.             vetos.fireVetoableChange("URL", oldValue, u);
  57.             url = u;
  58.             context = null;
  59.             changes.firePropertyChange("URL", oldValue, u);
  60.         }
  61.     }
  62.  
  63.     /**
  64.      * Returns the URL of the document to show when the button is clicked.
  65.      * @see #setURL
  66.      */
  67.     public URL getURL()
  68.     {
  69.         return url;
  70.     }
  71.  
  72.     /**
  73.      * Sets the frame specifier for showing a URL document in a browser or applet
  74.      * viewer. It is interpreted as follows:
  75.      * <UL>
  76.      * <DT>"_self"  show document in the current frame</DT>
  77.      * <DT>"_parent"    show document in the parent frame</DT>
  78.      * <DT>"_top"   show document in the topmost frame</DT>
  79.      * <DT>"_blank" show document in a new unnamed toplevel window</DT>
  80.      * <DT>all others   show document in a new toplevel window with the given name</DT>
  81.      * </UL>
  82.      * @param f the frame specifier
  83.      * @see #getFrame
  84.      * @see symantec.itools.util.GeneralUtils#frameTarget_self
  85.      * @see symantec.itools.util.GeneralUtils#frameTarget_parent
  86.      * @see symantec.itools.util.GeneralUtils#frameTarget_top
  87.      * @see symantec.itools.util.GeneralUtils#frameTarget_blank
  88.      * @exception PropertyVetoException
  89.      * if the specified property value is unacceptable
  90.      */
  91.     public void setFrame(String f) throws PropertyVetoException
  92.     {
  93.         String oldValue = frame;
  94.  
  95.         vetos.fireVetoableChange("Frame", oldValue, f);
  96.  
  97.         frame = f;
  98.  
  99.         changes.firePropertyChange("Frame", oldValue, f);
  100.     }
  101.  
  102.     /**
  103.      * Gets the frame specifier for showing a URL document in a browser or applet
  104.      * viewer. It is interpreted as follows:
  105.      * <UL>
  106.      * <DT>"_self"  show document in the current frame</DT>
  107.      * <DT>"_parent"    show document in the parent frame</DT>
  108.      * <DT>"_top"   show document in the topmost frame</DT>
  109.      * <DT>"_blank" show document in a new unnamed toplevel window</DT>
  110.      * <DT>all others   show document in a new toplevel window with the given name</DT>
  111.      * </UL>
  112.      * @return the frame specifier
  113.      * @see #setFrame
  114.      */
  115.     public String getFrame()
  116.     {
  117.         return frame;
  118.     }
  119.  
  120.     /**
  121.      * Tells this component that it has been added to a container.
  122.      * This is a standard Java AWT method which gets called by the AWT when
  123.      * this component is added to a container. Typically, it is used to
  124.      * create this component's peer.
  125.      *
  126.      * It has been overridden here to hook-up event listeners.
  127.      *
  128.      * @see #removeNotify
  129.      */
  130.     public synchronized void addNotify()
  131.     {
  132.         super.addNotify();
  133.  
  134.         //Hook up listeners
  135.         if (action == null)
  136.         {
  137.             action = new Action();
  138.             addActionListener(action);
  139.         }
  140.         if (mouse == null)
  141.         {
  142.             mouse = new Mouse();
  143.             addMouseListener(mouse);
  144.         }
  145.  
  146.         // On addNotify, try to find the containing applet.
  147.         Container c = getParent();
  148.         while (c != null)
  149.         {
  150.             if (c instanceof Applet)
  151.             {
  152.                 setAppletContext(((Applet) c).getAppletContext());
  153.                 break;
  154.             }
  155.  
  156.             c = c.getParent();
  157.         }
  158.     }
  159.  
  160.     /**
  161.      * Tells this component that it is being removed from a container.
  162.      * This is a standard Java AWT method which gets called by the AWT when
  163.      * this component is removed from a container. Typically, it is used to
  164.      * destroy the peers of this component and all its subcomponents.
  165.      *
  166.      * It has been overridden here to unhook event listeners.
  167.      *
  168.      * @see #addNotify
  169.      */
  170.     public synchronized void removeNotify()
  171.     {
  172.         //Unhook listeners
  173.         if (action != null)
  174.         {
  175.             removeActionListener(action);
  176.             action = null;
  177.         }
  178.         if (mouse != null)
  179.         {
  180.             removeMouseListener(mouse);
  181.             mouse = null;
  182.         }
  183.  
  184.         super.removeNotify();
  185.     }
  186.  
  187.     /**
  188.      * Adds a listener for all event changes.
  189.      * @param PropertyChangeListener listener the listener to add.
  190.      * @see #removePropertyChangeListener
  191.      */
  192.     public synchronized void addPropertyChangeListener(PropertyChangeListener listener)
  193.     {
  194.         super.addPropertyChangeListener(listener);
  195.         changes.addPropertyChangeListener(listener);
  196.     }
  197.  
  198.     /**
  199.      * Removes a listener for all event changes.
  200.      * @param PropertyChangeListener listener the listener to remove.
  201.      * @see #addPropertyChangeListener
  202.      */
  203.     public synchronized void removePropertyChangeListener(PropertyChangeListener listener)
  204.     {
  205.         super.removePropertyChangeListener(listener);
  206.         changes.removePropertyChangeListener(listener);
  207.     }
  208.  
  209.     /**
  210.      * Adds a vetoable listener for all event changes.
  211.      * @param VetoableChangeListener listener the listener to add.
  212.      * @see #removeVetoableChangeListener
  213.      */
  214.     public synchronized void addVetoableChangeListener(VetoableChangeListener listener)
  215.     {
  216.         super.addVetoableChangeListener(listener);
  217.         vetos.addVetoableChangeListener(listener);
  218.     }
  219.  
  220.     /**
  221.      * Removes a vetoable listener for all event changes.
  222.      * @param VetoableChangeListener listener the listener to remove.
  223.      * @see #addVetoableChangeListener
  224.      */
  225.     public synchronized void removeVetoableChangeListener(VetoableChangeListener listener)
  226.     {
  227.         super.removeVetoableChangeListener(listener);
  228.         vetos.removeVetoableChangeListener(listener);
  229.     }
  230.  
  231.     /**
  232.      * This is the Mouse Event handling innerclass.
  233.      */
  234.     class Mouse extends java.awt.event.MouseAdapter
  235.     {
  236.         boolean statusChanged = false;
  237.         
  238.         /**
  239.          * Handles Mouse Entered events
  240.          * @param e the MouseEvent
  241.          */
  242.         public void mouseEntered(MouseEvent e)
  243.         {
  244.             if(context != null && url != null)
  245.             {
  246.                 statusChanged = true;
  247.                 context.showStatus(url.toString());
  248.             }
  249.         }        
  250.  
  251.         /**
  252.          * Handles Mouse Exited events
  253.          * @param e the MouseEvent
  254.          */
  255.         public void mouseExited(MouseEvent e)
  256.         {
  257.             if(context != null && statusChanged)
  258.             {
  259.                 statusChanged = false;
  260.                 context.showStatus("");
  261.             }
  262.         }        
  263.     }
  264.  
  265.     /**
  266.      * This is the Adjustment Event handling innerclass.
  267.      */
  268.     class Action implements java.awt.event.ActionListener, java.io.Serializable
  269.     {
  270.         /**
  271.          * Handles Action events
  272.          * @param e the ActionEvent
  273.          */
  274.         public void actionPerformed(ActionEvent e)
  275.         {
  276.             if (context != null && url != null)
  277.             {
  278.                 if (frame == null || frame.length() == 0)
  279.                     context.showDocument(url);
  280.                 else
  281.                     context.showDocument(url, frame);
  282.             }
  283.         }
  284.     }
  285.  
  286.     /**
  287.      * Sets the applet context used to view documents.
  288.      * @param c the new applet context
  289.      */
  290.     protected void setAppletContext(AppletContext c)
  291.     {
  292.         context = c;
  293.     }
  294.  
  295.     /**
  296.      * The URL of the document to show when the button is clicked.
  297.      */
  298.     protected URL url;
  299.  
  300.     /**
  301.      * Applet context that shows the document.
  302.      */
  303.     transient protected AppletContext context;
  304.  
  305.     /**
  306.      * Frame specifier for showing a URL document in a browser or applet
  307.      * viewer. It is interpreted as follows:
  308.      * <UL>
  309.      * <DT>"_self"  show document in the current frame</DT>
  310.      * <DT>"_parent"    show document in the parent frame</DT>
  311.      * <DT>"_top"   show document in the topmost frame</DT>
  312.      * <DT>"_blank" show document in a new unnamed toplevel window</DT>
  313.      * <DT>all others   show document in a new toplevel window with the given name</DT>
  314.      * </UL>
  315.      */
  316.     protected String frame;
  317.  
  318.     // Private members
  319.     private Action    action = null;
  320.     private Mouse    mouse = null;
  321.     private symantec.itools.beans.VetoableChangeSupport vetos = new symantec.itools.beans.VetoableChangeSupport(this);
  322.     private symantec.itools.beans.PropertyChangeSupport changes = new symantec.itools.beans.PropertyChangeSupport(this);
  323. }
  324.  
  325.